package com.asiainfo.order.service.impl;


import com.asiainfo.entity.order.Order;
import com.asiainfo.entity.product.Product;
import com.asiainfo.order.feign.ProductFeign;
import com.asiainfo.order.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

@Slf4j
@Service
public class ImplOrderService implements OrderService {
    //SpringBoot提供了通用调用HTTP的工具
    @Autowired
    RestTemplate restTemplate;

    @Autowired
    DiscoveryClient discoveryClient;

    //负载均衡对象
    @Autowired
    LoadBalancerClient loadBalancerClient;

    @Autowired
    ProductFeign productFeign;
    /**
     * 调用商品信息返回Order
     * @param userId
     * @param productId
     * @return
     */
    @Override
    public Order createOrder(Long userId, Long productId) {

        //调用商品接口获取数据
//        Product product = getProductFromRemoteBlanclerAnnatation(productId);

        //通过openfeign调用
        Product product = productFeign.getProductById(productId);
        Order order = Order.builder()
                .id(1L)
                .userId(userId)
                .address("重庆")
                .nickName("zhangshan")
                //需要从商品服务获取后计算
                .totalAmount(product.getPrice().multiply(new BigDecimal(product.getNum())))
                .productList(Arrays.asList(product))
                .build();
        return order;
    }

    public Product getProductFromRemote( Long productId){

        //获取服务所在的主机和端口
        List<ServiceInstance> instances = discoveryClient.getInstances("product-order");
        ServiceInstance serviceInstance = instances.get(0);
        //拼接URL
        String url = "http://"+serviceInstance.getHost() + ":" + serviceInstance.getPort() + "/product/" + productId;
        log.info("调用商品接口："+url);
        Product product = restTemplate.getForObject(url, Product.class);
        return product;
    }

    //添加负载均衡
    public Product getProductFromRemoteBlancler( Long productId){

        //获取服务所在的主机和端口 根据服务名称均衡的选择实例
        ServiceInstance serviceInstance = loadBalancerClient.choose("product-order");
//        ServiceInstance serviceInstance = instances.get(0);
        //拼接URL
        String url = "http://"+serviceInstance.getHost() + ":" + serviceInstance.getPort() + "/product/" + productId;
        log.info("调用商品接口："+url);
        Product product = restTemplate.getForObject(url, Product.class);
        return product;
    }

    //注解的方式实现负载均衡
    public Product getProductFromRemoteBlanclerAnnatation( Long productId){
        //restTempLate注入负载均衡后，会解析服务名称自动获取
        String url = "http://product-order/product/" + productId;
        log.info("调用商品接口："+url);
        Product product = restTemplate.getForObject(url, Product.class);
        return product;
    }
}
